<?php
header("content-type:text/html;charset=utf8");
$id =isset($_POST['id'])?$_POST['id']:"";
//print_r($shop_id);die;
$redis=new Redis();
$redis->connect("127.0.0.1","6379");
$pdo =new PDO("mysql:host=127.0.0.1;dbname=goods;charset=utf8","root","root");
$sql = "select stock from good where id = $id";
$res = $pdo->query($sql)->fetch();
// for($i=1;$i<=$res['stock'];$i++){
// 	$redis->lpush("$data".$id,$i);
// }
if ($redis->lpop("$data".$id))
{
    $sql = "update good set stock = stock-1 where id = $id";
    $info = $pdo->exec($sql);
    if($info)
    {
        $data = array('code' =>1,"msg"=>"抢购成功");
    }
}else{
    $data = array('code' =>1,"msg"=>"对不起，抢光了");
}
echo json_encode($data);
?>